<?php
include("./config.php");
if ($_SERVER["REQUEST_METHOD"] == "POST") {

	$userNum = mysql_real_escape_string($_POST['UserNum']);
	$gameType = mysql_real_escape_string($_POST['EndGameType']);

	if($gameType == "Stage"){
		$clearStage = mysql_real_escape_string($_POST['ClearStage']);
		$sql = "update user_info_table set ClearStage=$clearStage where UserNum=$userNum";		
		$result = mysql_query($sql);
		
		if($result == '1') { 
			echo '1'; 
		}
		else { 
			echo '-1'; 
		}
		
	}else{
		$cellStack = mysql_real_escape_string($_POST['CellStack']);

		if($gametype == "Win") {
			$sql = "select Win from user_info_table where UserNum='$userNum'";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			$win = $row['Win'] + 1;

			$sql = "update user_info_table set Win='$win' where UserNum='$userNum'";
			$result = mysql_query($sql);
		}
		else if($gametype == "Loss"){
			$sql = "select Loss from user_info_table where UserNum='$userNum'";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			$loss = $row['Loss'] + 1;

			$sql = "update user_info_table set Loss='$loss' where UserNum='$userNum'";
			$result = mysql_query($sql);
		}
		else if($gametype == "Draw"){
			$sql = "select Draw from user_info_table where UserNum='$userNum'";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			$draw = $row['Draw'] + 1;

			$sql = "update user_info_table set Draw='$draw' where UserNum='$userNum'";
			$result = mysql_query($sql);
		}

		$sql = "select CellStack from user_info_table where UserNum='$userNum'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$cellStack = $row['CellStack'] + $cellStack;

		$sql = "update user_info_table set CellStack='$cellStack' where UserNum='$userNum'";
		$result = mysql_query($sql) or die(-1);

		echo 1;
	}

} else if ($_SERVER["REQUEST_METHOD"] == "GET") {

	$userNum = mysql_real_escape_string($_GET['UserNum']);
	$sql = "select * from USER_INFO_TABLE where UserNum='$userNum'";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	$win = $row['Win'];
	$loss = $row['Loss'];
	$draw = $row['Draw'];
	$cellstack = $row['CellStack'];
	$coin = $row['Coin'];
	$clearStage = $row['ClearStage'];

	$sql = "select * from USER_TABLE where UserNum=$userNum";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	$id = $row['Id'];

	echo $id."$".$win."$".$loss."$".$draw."$".$cellstack."$".$coin."$".$clearStage;

}
?>